<?php
declare (strict_types = 1);
/**
 * 管理员
 */
namespace app\common\admin;

use app\common\facade\model\ProgramModel;
use app\common\facade\model\AdvseqModel;
use app\common\facade\model\AdminModel;
use app\common\facade\model\ChannelInfoModel;

class Program extends BaseAdmin
{

    /* 节目时段列表 */
    public function pgmseqList($param = []) {
        $where = [];

        if (isset($param['seg_name']) && !empty($param['seg_name'])) $where[] = ['seg_name', 'like', "%{$param['seg_name']}%"];
        if (isset($param['channel_id']) && !empty($param['channel_id'])) $where[] = ['channel_id','=',"{$param['channel_id']}"];

        $pgmseqList = ProgramModel::pgmseqList($where, $param['offset'], $param['length']);

        return $pgmseqList;
    }

    /* 节目时段编辑 */
    public function pgmseqEdit($param = []) {
        $where = [];
        $adminLoginInfo = $this->adminLoginInfo();
        $ch_info = ChannelInfoModel::info(['id'=>$param['channel_id']]);
        if(isset($param['pgm_seq_id'])){
            $where[] = ['pgm_seq_id', '=', $param['pgm_seq_id']];
            unset($param['pgm_seq_id']);

            // 执行修改广告节目时段日志
            $this->syslog('pro','用户['.$adminLoginInfo['username'].'] 修改节目时段 ['.$ch_info['channel_name'].']-['.$param['seg_name'].']！',$adminLoginInfo);
        }else{
            // 执行增加广告节目时段日志
            $this->syslog('pro','用户['.$adminLoginInfo['username'].'] 新增节目时段 ['.$ch_info['channel_name'].']-['.$param['seg_name'].']！',$adminLoginInfo);
        }

        return ProgramModel::addEditSave($param, $where);
    }

    /* 节目时段删除 */
    public function pgmseqDel($ids) {
        // 当ids 不为空时
        if(!empty($ids)) {
            // 增加删除日志
            $adminLoginInfo = $this->adminLoginInfo();
            $id_arr = explode(',', $ids);
            foreach ($id_arr as $k => $val) {
                $info = ProgramModel::info(['pgm_seq_id'=>$val]);
                $ch_info = ChannelInfoModel::info(['id'=>$info['channel_id']]);
                // 执行删除广告节目时段日志
                $this->syslog('pro','用户['.$adminLoginInfo['username'].'] 删除节目时段 ['.$ch_info['channel_name'].']-['.$info['seg_name'].']！',$adminLoginInfo);
            }

            $res = ProgramModel::deleteData([['pgm_seq_id', 'in', $ids]]);
        } else {
            return [
                'code' => 0,
                'msg'  => '参数未定义'
            ];
        }

        return [
            'code' => $res ? 1 : 0,
            'msg'  => $res ? '删除成功' : '删除失败'
        ];
    }

    /* 节目时段范围查询 */
    public function pgmseqBetween($pgm_seq_id,$s_time,$e_time,$channel_id) {
        $where = 'channel_id='.$channel_id;

        if (isset($pgm_seq_id)) {
            $where .= ' AND pgm_seq_id!='.$pgm_seq_id;
        }
        if (!empty($s_time) && !empty($e_time)){
            $where .= " AND ((s_time >= ".$s_time." AND s_time < ".$e_time.") OR";
            $where .= " (s_time < ".$s_time." AND e_time > ".$e_time.") OR";
            $where .= " (e_time > ".$s_time." AND e_time <= ".$e_time."))";
        }
        
        $pgmseqList = ProgramModel::pgmseqBt($where);

        return $pgmseqList;
    }


    /* 节目时段列表 */
    public function advseqList($param = []) {
        $where = [];

        if (isset($param['adv_seg_name']) && !empty($param['adv_seg_name'])) $where[] = ['adv_seg_name', 'like', "%{$param['adv_seg_name']}%"];
        if (isset($param['channel_id']) && !empty($param['channel_id'])) $where[] = ['channel_id','=',"{$param['channel_id']}"];
        if ($param['time_status'] == 1){ $where[] = ['date_limit','>',"0"]; }
        if ($param['time_status'] == 2){ 
            $where[] = ['date_limit','>=',"1"];
            $where[] = ['s_date','<=',date("Y-m-d")];
            $where[] = ['e_date','>=',date("Y-m-d")];
        }
        if ($param['time_status'] == 3){
            $where[] = ['date_limit','=',"2"];
            $where[] = ['e_date','<',date("Y-m-d")];
        }

        $pgmseqList = AdvseqModel::advseqList($where, $param['offset'], $param['length']);

        foreach ($pgmseqList['list'] as $k => $val) {
            // 权限控制
            if (!empty($val['oper_users'])) {
                $oper_arr = explode(",", $val['oper_users']);
                $user_arr = array();
                for ($i=0; $i < count($oper_arr); $i++) {
                    // 用户详情
                    $where = array();
                    $where[] = ['id', '=', $oper_arr[$i]];
                    $user_arr[] = AdminModel::adminInfo($where);
                }
                $pgmseqList['list'][$k]['oper_users_arr'] = $user_arr;
            }
        }
        
        return $pgmseqList;
    }

    /* 广告节目时段编辑 */
    public function advseqEdit($param = []) {
        $where = [];
        $adminLoginInfo = $this->adminLoginInfo();
        $ch_info = ChannelInfoModel::info(['id'=>$param['channel_id']]);

        if(isset($param['adv_seq_id'])){
            $where[] = ['adv_seq_id', '=', $param['adv_seq_id']];
            unset($param['adv_seq_id']);

            // 执行修改广告节目时段日志
            $this->syslog('pro','用户['.$adminLoginInfo['username'].'] 修改广告节目时段 ['.$ch_info['channel_name'].']-['.$param['adv_seg_name'].']！',$adminLoginInfo);
        }else{
            // 执行新增广告节目时段日志
            $this->syslog('pro','用户['.$adminLoginInfo['username'].'] 新增广告节目时段 ['.$ch_info['channel_name'].']-['.$param['adv_seg_name'].']！',$adminLoginInfo);
        }
  
        return AdvseqModel::addEditSave($param, $where);
    }

    /* 广告节目时段删除 */
    public function advseqDel($ids) {
        // 当ids 不为空时
        if(!empty($ids)) {
            // 增加删除日志
            $adminLoginInfo = $this->adminLoginInfo();
            $id_arr = explode(',', $ids);
            foreach ($id_arr as $k => $val) {
                $info = AdvseqModel::info(['adv_seq_id'=>$val]);
                $ch_info = ChannelInfoModel::info(['id'=>$info['channel_id']]);
                // 执行删除广告节目时段日志
                $this->syslog('pro','用户['.$adminLoginInfo['username'].'] 删除广告节目时段 ['.$ch_info['channel_name'].']-['.$info['adv_seg_name'].']！',$adminLoginInfo);
            }
            
            $res = AdvseqModel::deleteData([['adv_seq_id', 'in', $ids]]);
        } else {
            return [
                'code' => 0,
                'msg'  => '参数未定义'
            ];
        }

        return [
            'code' => $res ? 1 : 0,
            'msg'  => $res ? '删除成功' : '删除失败'
        ];
    }

}